app.controller("billingStatisticsCtrl", function ($scope,$interval, $http, billingStatisticsService) {

    //----------分页参数------------
    $scope.pageRowSize = 10;
    $scope.curPage = 1;

    //-----------------------------

    $scope.object = {};
    $scope.formMode = false;
    $scope.errors = {};

    //--------------初始化菜单---------------
    $scope.initMenu = function () {
        showSecondMenu('billingManage');
        selectSecondMenu('billingStatisticsQuery');
    }
    $scope.initMenu();

    //---------------错误信息--------------
    $scope.requestError = function () {
        $scope.errorMessage = "unknown error!"
    };

    $scope.init = function () {
        $scope.endMonth = getMonthFunction(0);  //开始日期
    };

    //查询账单
    $scope.statistics = function (contractUserId) {
        $.fn.jqLoading({ height: 100, width: 240, text: "查询中，请稍等...." });
        if(NullOrUndefined($("#endMonth").val())){
            promptMsg("请选择月份！");
            return false;
        }
        billingStatisticsService.statistics({
            action: 'statistics',
            endMonth: $("#endMonth").val(),
            contractUserId: contractUserId
        }, function (data) {
            $.fn.jqLoading("destroy");
            if (data.status == "SUCCESS") {
                $scope.contractUsers = data.data.contractUsers;
                $scope.contractUserId = data.data.contractUserId;
                $scope.title = data.data.title;       //标题
                $scope.year = data.data.year;       //年份
                $scope.excelItems = data.data.excelItems;

                $scope.endMonthVal = $("#endMonth").val();
            } else {
                promptMsg(data.errorReason);
            }
        }, $scope.requestError());
    }

    //导出Excel文件
    $scope.exportExcel = function () {
        if(NullOrUndefined($scope.endMonthVal)){
            promptMsg("请统计后再进行导出！");
            return false;
        }

        billingStatisticsService.checkexcel({
            action: 'checkexcel',
            endMonth: $("#endMonth").val()
        }, function (data) {
            $.fn.jqLoading("destroy");
            var msg = data.data.msg;
            if (NullOrUndefined(msg)) {
                window.location.href = "/billingstatistics/excel?endMonth=" + $("#endMonth").val();
            } else {
                promptMsg(msg);
            }
        }, $scope.requestError());

        //window.location.href = "/billingstatistics/excel?endMonth=" + $("#endMonth").val();
    }
});